import configuration
import download
import downloadgroup2
import processInformation


def showfeedback(totalbytes, bytesread):
	"""Just to show some information about the download"""
	import os
	os.system('cls')
	print 'Downloaded %d from %d' % (bytesread, totalbytes)

class UpdateManager(object):
	def __init__(self):
		self.config = configuration.Configuration()
		
	def update(self):
		"""Method that try to do the update, the update starts here!		
		Returns 0: Is Up to date
		1:Not updated and the files were downloaded
		2:Not updated and the files weren't downloaded
		"""		
		
		if not self.isupdated():
			if self.downloadfiles():
				self.config.update_installed_version()
				return 1
			else:
				return 2
		else:
			return 0
			
	def extractfiles(self):
		pass
				
	def isupdated(self):
		local_version = self.config.get_installed_version()
		remote_version = self.config.get_server_version()
		return local_version >= remote_version
		
				
	def checkprocess(self):
		return processInformation.check_process_running(self.config.get_program_name())		
			
		
	
	def downloadfiles(self, feedback = None):
		"""Try to do the download of the files in the adresses provided by the config"""
		exeaddress = self.config.get_exefile_address()
		documentsaddress = self.config.get_documents_address()
		#try:
		self.downloads = [] #stores the downloads objects
		exedownload = download.Download(exeaddress, 'SBArrecadacao.zip')
		documentsdownload = download.Download(documentsaddress, 'Documentos.zip')
		self.downloads.append(exedownload)
		self.downloads.append(documentsdownload)
		self.downloadgroup = downloadgroup2.DownloadGroup2(self.downloads, feedback)
		self.downloadgroup.start()
		#	return True
		#except:
		#	return False
		return True